home *** CD-ROM | disk | FTP | other *** search
/ 10,000 Great Games / 10,000 Great Games.iso / Product / 66 / data1.cab / Source_Files / Src / Mouse.cpp < prev    next >
C/C++ Source or Header  |  2000-01-16  |  6KB  |  303 lines

  1. #include "stdafx.h"
  2.  
  3. int reverse_mouse;
  4.  
  5. static LPDIRECTINPUTDEVICE2 mouse = 0;
  6. static DIMOUSESTATE mousestate;
  7.  
  8. void init_mouse()
  9.     mouse = create_input_device_mouse();
  10.  
  11.     acquire_mouse();
  12. }
  13.  
  14. void acquire_mouse()
  15. {
  16. #ifndef _DEBUG
  17.     mouse->Acquire();
  18. #endif
  19. }
  20.  
  21. void deinit_mouse()
  22. {
  23.     if (mouse != 0)
  24.     {
  25.         mouse->Unacquire();
  26.     
  27.         mouse->Release();
  28.     }
  29. }
  30.  
  31. LPDIRECTINPUTDEVICE2 create_input_device_mouse()
  32. {
  33.     LPDIRECTINPUTDEVICE2 mouse;
  34.  
  35.     mouse = create_input_device(GUID_SysMouse);
  36.     
  37.     if (FAILED(mouse->SetCooperativeLevel(mainwindowhandle, DISCL_EXCLUSIVE | DISCL_FOREGROUND)))
  38.         error("Unable to set cooperative level for mouse");
  39.  
  40.     if (FAILED(mouse->SetDataFormat(&c_dfDIMouse)))
  41.         error("Unable to set data format for mouse");
  42.     
  43.     DIPROPDWORD dipdw;
  44.     
  45.     dipdw.diph.dwSize = sizeof(DIPROPDWORD);
  46.     dipdw.diph.dwHeaderSize = sizeof(DIPROPHEADER);
  47.     dipdw.diph.dwObj = 0;
  48.     dipdw.diph.dwHow = DIPH_DEVICE;
  49.     dipdw.dwData = MOUSE_BUFFERSIZE;
  50.     
  51.     if (FAILED(mouse->SetProperty(DIPROP_BUFFERSIZE, &dipdw.diph)))
  52.         error("Unable to set mouse buffer size");
  53.  
  54.     return mouse;
  55. }
  56.  
  57. cMouse::cMouse()
  58. {
  59.     // Set image
  60.         
  61.     set_image(in_game_pointer);
  62.     
  63.     // Reset mouse state
  64.     
  65.     reset();
  66. }
  67.  
  68. cMouse::~cMouse()
  69. {
  70. }
  71.  
  72. void cMouse::reset()
  73. {
  74.     // Reset mickey counters
  75.     
  76.     mx = 0, my = 0;
  77.     
  78.     // Update mouse buttons
  79.     
  80.     bl_last = 0;
  81.     bl_double = FALSE;
  82. }
  83.  
  84. void cMouse::determine(cPlayer *p)
  85. {
  86.     // Get the state of the mouse
  87.     
  88.     if (FAILED(mouse->GetDeviceState(sizeof(mousestate), &mousestate)))
  89.     {
  90.         // Mouse input was lost, reacquire
  91.         
  92.         mouse->Acquire();
  93.         
  94.         if (FAILED(mouse->GetDeviceState(sizeof(mousestate), &mousestate)))
  95.             return;
  96.     }
  97.     
  98.     // Compute new x and y mickey count
  99.     
  100.     mx += mousestate.lX, my += mousestate.lY;
  101.     
  102.     if (mx < -MICKEYS_XMAX)
  103.         mx = -MICKEYS_XMAX;
  104.     else if (mx > MICKEYS_XMAX)
  105.         mx = MICKEYS_XMAX;
  106.     
  107.     if (my < -MICKEYS_YMAX)
  108.         my = -MICKEYS_YMAX;
  109.     else if (my > MICKEYS_YMAX)
  110.         my = MICKEYS_YMAX;
  111.     
  112.     // Compute new button state
  113.     
  114.     int bl = mousestate.rgbButtons[reverse_mouse? 1:0] & 0x80,
  115.         br = mousestate.rgbButtons[reverse_mouse? 0:1] & 0x80;    
  116.     
  117.     // Get the state in a buffer for checking doubleclicks
  118.     
  119.     DIDEVICEOBJECTDATA dod[MOUSE_BUFFERSIZE];
  120.     DWORD items = MOUSE_BUFFERSIZE;
  121.     
  122.     if (FAILED(mouse->GetDeviceData(sizeof(DIDEVICEOBJECTDATA), dod, &items, 0)))
  123.     {
  124.         // We lost mouse input, reacquire
  125.         
  126.         mouse->Acquire();
  127.         
  128.         if (FAILED(mouse->GetDeviceData(sizeof(DIDEVICEOBJECTDATA), dod, &items, 0)))
  129.         {
  130.             // Unable to reacquire, reset button info
  131.             
  132.             bl_last = 0;
  133.             bl_double = FALSE;
  134.             
  135.             return;
  136.         }
  137.     }
  138.     
  139.     for (DWORD d = 0; d < items; d++)
  140.     {
  141.         // Check if this means left button is pressed
  142.         
  143.         if ((reverse_mouse? dod[d].dwOfs == DIMOFS_BUTTON1 : dod[d].dwOfs == DIMOFS_BUTTON0) && (dod[d].dwData & 0x80))
  144.         {
  145.             
  146.             if (dod[d].dwTimeStamp - bl_last <= MOUSE_DCLICKTIME)
  147.             {
  148.                 // This is a double click
  149.                 
  150.                 bl_last = 0;
  151.                 bl_double = TRUE;
  152.             }
  153.             else
  154.             {
  155.                 // It's not a double click, but remember last time this key was pressed
  156.                 
  157.                 bl_last = dod[d].dwTimeStamp;
  158.             }
  159.         }
  160.     }
  161.     
  162.     // Make discrete states
  163.     
  164.     int    half_left         = mx < -MICKEYS_XHALF,
  165.         half_right        = mx > MICKEYS_XHALF,
  166.         
  167.         is_up            = my < -MICKEYS_YFULL,
  168.         is_down            = my > MICKEYS_YFULL,
  169.         is_left            = mx < -MICKEYS_XFULL,
  170.         is_right        = mx > MICKEYS_XFULL,
  171.         
  172.         confirmed_up    = is_up && bl,
  173.         confirmed_down    = is_down && bl,
  174.         confirmed_left    = is_left && bl,
  175.         confirmed_right    = is_right && bl;
  176.     
  177.     // Set mouse pointer
  178.     
  179.     make_dirty();
  180.     set_position(p->x + mx * 50 / MICKEYS_XMAX, p->y + 20 - my * 50 / MICKEYS_YMAX);    
  181.     make_dirty();
  182.     
  183.     // Cannot do anything when not active
  184.     
  185.     if (!p->is_active())
  186.         return;
  187.     
  188.     // Check fire button
  189.     
  190.     if (br)
  191.     {
  192.         if (is_up && !is_left && !is_right)
  193.             p->fire_up();
  194.         else if (is_down && !half_left && !half_right)
  195.             p->fire_down();
  196.         else
  197.             p->fire();
  198.     }
  199.     
  200.     // Cannot do anything more when captured
  201.     
  202.     if (p->is_captured())
  203.         return;
  204.     
  205.     // Jetpack
  206.     
  207.     if (bl_double)
  208.     {
  209.         p->jet_on();
  210.         
  211.         bl_double = FALSE;
  212.     }
  213.     
  214.     // Do movement
  215.     
  216.     if (p->is_walking())
  217.     {
  218.         if (half_left)
  219.             p->walk_turn_left();
  220.         else if (half_right)
  221.             p->walk_turn_right();
  222.         
  223.         if (confirmed_left)
  224.             p->walk_left();
  225.         else if (confirmed_right)
  226.             p->walk_right();
  227.         else
  228.             p->walk_h_halt();
  229.         
  230.         if (confirmed_up)
  231.             p->jump();
  232.         else if (is_down)
  233.             p->duck();
  234.     }
  235.     else if (p->is_ducked())
  236.     {
  237.         if (!is_down)
  238.             p->stand();
  239.         
  240.         if (half_left)
  241.             p->duck_left();
  242.         else if (half_right)
  243.             p->duck_right();
  244.     }
  245.     else if (p->is_jumping())
  246.     {
  247.         if (half_left)
  248.             p->jump_turn_left();
  249.         else if (half_right)
  250.             p->jump_turn_right();
  251.         
  252.         if (confirmed_left)
  253.             p->jump_left();
  254.         else if (confirmed_right)
  255.             p->jump_right();
  256.         else
  257.             p->jump_h_halt();
  258.     }
  259.     else if (p->is_jetting())
  260.     {    
  261.         if (confirmed_up)
  262.             p->jet_up();
  263.         else if (is_down)
  264.             p->jet_off();
  265.         else
  266.             p->jet_v_halt();
  267.         
  268.         if (half_left)
  269.             p->jet_turn_left();
  270.         else if (half_right)
  271.             p->jet_turn_right();
  272.         
  273.         if (confirmed_left)
  274.             p->jet_left();
  275.         else if (confirmed_right)
  276.             p->jet_right();
  277.         else
  278.             p->jet_h_halt();
  279.     }
  280.     else if (p->is_climbing())
  281.     {
  282.         if (confirmed_up)
  283.             p->climb_up();
  284.         else if (confirmed_down)
  285.             p->climb_down();
  286.         else
  287.             p->climb_v_halt();
  288.         
  289.         if (half_left)
  290.             p->climb_turn_left();
  291.         else if (half_right)
  292.             p->climb_turn_right();
  293.         
  294.         if (confirmed_left)
  295.             p->climb_left();
  296.         else if (confirmed_right)
  297.             p->climb_right();
  298.         else
  299.             p->climb_h_halt();
  300.     }
  301. }
  302.